package com.pro.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.dao.BookMapper;
import com.pro.dao.EvaluationMapper;
import com.pro.dao.MemberMapper;
import com.pro.domain.Book;
import com.pro.domain.Evaluation;
import com.pro.domain.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EvaluationServiceImpl implements EvaluationService {
    @Autowired
    private EvaluationMapper evaluationMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private MemberMapper memberMapper ;

    /**
     * 根据图书Id，拿到评论
     * @param bookId
     * @return
     */
    @Override
    public List<Evaluation> getEvaluationByBookId(Long bookId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("book_id",bookId);
        queryWrapper.eq("state","enable");
        queryWrapper.orderByDesc("create_time");

        List<Evaluation> evaluationList = evaluationMapper.selectList(queryWrapper);

        Book book = bookMapper.selectById(bookId);
        for (Evaluation evaluation : evaluationList) {
            Member member = memberMapper.selectById(evaluation.getMemberId());
            evaluation.setMember(member);
            evaluation.setBook(book);
        }
        return evaluationList;
    }
}
